/*
   Copyright (c) 2023 李伟国
   learner-platform is licensed under Mulan PSL v2.
   You can use this software according to the terms and conditions of the Mulan PSL v2. 
   You may obtain a copy of Mulan PSL v2 at:
               http://license.coscl.org.cn/MulanPSL2 
   THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.  
   See the Mulan PSL v2 for more details. 
*/

package cn.platform.controller;

import cn.platform.entity.LearningPlan;
import cn.platform.exception.RequestParameterMissingException;
import cn.platform.response.ResponseData;
import cn.platform.response.SuccessResponseData;
import cn.platform.service.ILearningPlanService;
import cn.platform.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@RestController
@RequestMapping("/learning/plan")
public class LearningPlanController extends BaseController {

    @Autowired
    private ILearningPlanService learningPlanService;

    @RequestMapping(value = "/{userId}", method = RequestMethod.GET)
    public ResponseData getByUserId(@PathVariable("userId") Long userId){
        return new SuccessResponseData(learningPlanService.findByUserId(userId));
    }

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public ResponseData getByUserId(){
        Long userId = UserUtil.getUserId();
        if(userId==null){
            throw new RequestParameterMissingException();
        }
        return new SuccessResponseData(learningPlanService.findByUserId(userId));
    }

    @RequestMapping(value = "/", method = RequestMethod.POST)
    public ResponseData save(@RequestBody LearningPlan... learningPlans){
        System.out.println(learningPlans.length);
        boolean f = true;
        Long userId = UserUtil.getUserId();
//        f = learningPlanService.saveBatch(Arrays.asList(learningPlans));
        for(LearningPlan l : learningPlans){
            l.setUserId(userId);
            f &= learningPlanService.save(l);
        }
        SUCCESS_INFO.setData(f);
        return SUCCESS_INFO;
    }

    @RequestMapping(value = "/", method = RequestMethod.DELETE)
    public ResponseData delete(@RequestBody LearningPlan... learningPlans){
        boolean f;
        Long[] userIds = Arrays.stream(learningPlans)
                .map(LearningPlan::getId)
                .distinct()
                .toArray(Long[]::new);
        f = learningPlanService.removeByUserId(userIds);
        SUCCESS_INFO.setData(f);
        return SUCCESS_INFO;
    }

    @RequestMapping(value = "/completed", method = RequestMethod.PUT)
    public ResponseData completed(@RequestBody LearningPlan... learningPlans){
        boolean f;
        Long[] planIds = Arrays.stream(learningPlans)
                .map(LearningPlan::getId)
                .distinct()
                .toArray(Long[]::new);
        f = learningPlanService.completed(planIds);
        SUCCESS_INFO.setData(f);
        return SUCCESS_INFO;
    }

    @RequestMapping(value = "/enable", method = RequestMethod.PUT)
    public ResponseData enable(@RequestBody LearningPlan... learningPlans){
        boolean f;
        Long[] planIds = Arrays.stream(learningPlans)
                .map(LearningPlan::getId)
                .distinct()
                .toArray(Long[]::new);
        f = learningPlanService.enable(planIds);
        SUCCESS_INFO.setData(f);
        return SUCCESS_INFO;
    }

}
